﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BUS;

namespace QLDT
{
    public partial class frm_QLPhanCongChiTiet_GV : DevExpress.XtraEditors.XtraForm
    {
        private int id_tuan;
        private int id_pcdt;
        private bool TThemSua = true;
        public DTO_TaiKhoan taikhoan;

        public frm_QLPhanCongChiTiet_GV(DTO_TaiKhoan tk)
        {
            taikhoan = tk;
            InitializeComponent();

        }
        public void Binding()
        {

            txt_ID_PCDT.DataBindings.Clear();
            txt_ID_PCDT.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "ID_PCDT");

            txt_SVTH.DataBindings.Clear();
            txt_SVTH.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "MaSV");

            txt_GVHD.DataBindings.Clear();
            txt_GVHD.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "TenGV");

            txt_TenDT.DataBindings.Clear();
            txt_TenDT.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "TenDT");

            txt_CN.DataBindings.Clear();
            txt_CN.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "TenCN");

            txt_loaidt.DataBindings.Clear();
            txt_loaidt.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "TenloaiDT");

            txt_Khoa.DataBindings.Clear();
            txt_Khoa.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "MaKH");

            txt_NamBT.DataBindings.Clear();
            txt_NamBT.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "NamBD");

            txt_NamKT.DataBindings.Clear();
            txt_NamKT.DataBindings.Add("Text", grid_DA_DaLam.DataSource, "NamKT");
        }
        public void load()
        {
            grid_QTTH.DataSource = BUS_QTTH.Select_QTTH();

            lb_tuan.DataBindings.Clear();
            lb_tuan.DataBindings.Add("Text", grid_QTTH.DataSource, "ID_tuan");

            memo_yeucau.DataBindings.Clear();
            memo_yeucau.DataBindings.Add("Text", grid_QTTH.DataSource, "ND_Yeucau");

            date_yeucau.DataBindings.Clear();
            date_yeucau.DataBindings.Add("Text", grid_QTTH.DataSource, "NgayYeuCau");

            memo_baocao.DataBindings.Clear();
            memo_baocao.DataBindings.Add("Text", grid_QTTH.DataSource, "ND_Baocao");

            date_baocao.DataBindings.Clear();
            date_baocao.DataBindings.Add("Text", grid_QTTH.DataSource, "NgayBaoCao");

            createGridlookup();

        }

     
        private void frm_QLPhanCongChiTiet_GV_Load(object sender, EventArgs e)
        {
            load();
            loadPCDT_GV();
            Binding();


        }

        public void createGridlookup()
        {

            //gridLookUpMaKH 
            gridLookUp_MaKH.EditValue = null;
            gridLookUp_MaKH.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUp_MaKH.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUp_MaKH.Properties.ValueMember = "MaKH";
            gridLookUp_MaKH.Properties.DisplayMember = "MaKH";
            gridLookUp_MaKH.Properties.View.BestFitColumns();
            gridLookUp_MaKH.Properties.PopupFormWidth = 400;
            gridLookUp_MaKH.Properties.NullText = "Chọn khóa học";
            gridLookUp_MaKH.Properties.DataSource = BUS_KhoaHoc.SelectKhoaHoc();

            // gridLookUpMaLoaiDT
            gridLookUp_MaLoaiDT.EditValue = null;
            gridLookUp_MaLoaiDT.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUp_MaLoaiDT.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUp_MaLoaiDT.Properties.ValueMember = "MaLoaiDT";
            gridLookUp_MaLoaiDT.Properties.DisplayMember = "MaLoaiDT";
            gridLookUp_MaLoaiDT.Properties.View.BestFitColumns();
            gridLookUp_MaLoaiDT.Properties.PopupFormWidth = 400;
            gridLookUp_MaLoaiDT.Properties.NullText = "Chọn loại đề tài";
            gridLookUp_MaLoaiDT.Properties.DataSource = BUS_LoaiDT.selectLoaiDeTai();
            //gridLookUpMaLop

            gridLookUp_MaLop.EditValue = null;
            gridLookUp_MaLop.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUp_MaLop.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUp_MaLop.Properties.ValueMember = "Malop";
            gridLookUp_MaLop.Properties.DisplayMember = "Malop";
            gridLookUp_MaLop.Properties.View.BestFitColumns();
            gridLookUp_MaLop.Properties.PopupFormWidth = 400;
            gridLookUp_MaLop.Properties.NullText = "Chọn lớp";
            gridLookUp_MaLop.Properties.DataSource = BUS_Lop.SelectLop();


        }
        private void txt_ID_PCDT_EditValueChanged(object sender, EventArgs e)
        {
            if (gv_QTTH.RowCount == 0)
            {
                int sotuan = int.Parse(gv_DA_Dalam.GetRowCellValue(gv_DA_Dalam.FocusedRowHandle, "Sotuan").ToString());
                if (txt_ID_PCDT.Text == "")
                    return;
                int id1 = int.Parse(txt_ID_PCDT.Text);
                for (int i = 1; i <= sotuan; i++)
                {
                    BUS_QTTH.insertQTTH(id1, i);
                }
            }
            if (txt_ID_PCDT.Text == "")
                return;
            txt_SVTH.Text = taikhoan.TenNguoiDung;
            int id = int.Parse(txt_ID_PCDT.Text);
            grid_QTTH.DataSource = BUS_QTTH.Select_QTTH_ID(id);

            lb_tuan.DataBindings.Clear();
            lb_tuan.DataBindings.Add("Text", grid_QTTH.DataSource, "ID_tuan");

            memo_yeucau.DataBindings.Clear();
            memo_yeucau.DataBindings.Add("Text", grid_QTTH.DataSource, "ND_Yeucau");

            date_yeucau.DataBindings.Clear();
            date_yeucau.DataBindings.Add("Text", grid_QTTH.DataSource, "NgayYeuCau");

            memo_baocao.DataBindings.Clear();
            memo_baocao.DataBindings.Add("Text", grid_QTTH.DataSource, "ND_Baocao");

            date_baocao.DataBindings.Clear();
            date_baocao.DataBindings.Add("Text", grid_QTTH.DataSource, "NgayBaoCao");

        }

        private void radio_davadanglam_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadPCDT_GV();
            Binding();

        }

        private void simpleBt_luu_Click(object sender, EventArgs e)
        {

            if (!KTDulieu())
            {
                XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                return;
            }
            DTO_QTTH QTTH = new DTO_QTTH();
            QTTH.ND_Baocao = memo_baocao.Text;
            QTTH.ND_Yeucau = memo_yeucau.Text;
            QTTH.NgayBaoCao = DateTime.Parse(date_baocao.Text);
            QTTH.NgayYeuCau = DateTime.Parse(date_yeucau.Text);
            int id2 = int.Parse(txt_ID_PCDT.Text);
            int sotuan2 = int.Parse(lb_tuan.Text);
            if (BUS_QTTH.UpdateQTTH(QTTH, sotuan2, id2))
                load();
            else
                XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
        }

        private bool KTDulieu()
        {

           
            if (memo_yeucau.Text == "")
                return false;
            if (date_yeucau.Text == "")
                return false;
            if (date_baocao.Text == "")
                return false;

            return true;
        }

        private void gridLookUp_MaKH_EditValueChanged(object sender, EventArgs e)
        {
            if (gridLookUp_MaKH.EditValue != null)
            {
                string makh = gridLookUp_MaKH.EditValue.ToString();
                gridLookUp_MaLop.EditValue = null;
                gridLookUp_MaLop.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
                gridLookUp_MaLop.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
                gridLookUp_MaLop.Properties.ValueMember = "Malop";
                gridLookUp_MaLop.Properties.DisplayMember = "Malop";
                gridLookUp_MaLop.Properties.View.BestFitColumns();
                gridLookUp_MaLop.Properties.PopupFormWidth = 400;
                gridLookUp_MaLop.Properties.NullText = "Chọn lớp";
                gridLookUp_MaLop.Properties.DataSource = BUS_Lop.SelectLop_theoKhoaHoc(makh);
            }
            loadPCDT_GV();
        
        
        }
        public void loadPCDT_GV()
        {
            string makh = "%";
            string malop = "%";
            string maloaidt = "%";
            string magv = "%";
            bool dalam_danglam = true;
            if (radio_davadanglam.SelectedIndex == 0)
                dalam_danglam = false;
            else
                dalam_danglam = true;
            if (gridLookUp_MaKH.EditValue != null)
                makh = gridLookUp_MaKH.EditValue.ToString();
            if (gridLookUp_MaLop.EditValue != null)
                malop = gridLookUp_MaLop.EditValue.ToString();
            if (gridLookUp_MaLoaiDT.EditValue != null)
                maloaidt = gridLookUp_MaLoaiDT.EditValue.ToString();
  
            grid_DA_DaLam.DataSource = BUS_PhanCongDT.PCDT_Select_Chamdiem_GV(taikhoan.TenTK, makh, malop, maloaidt, dalam_danglam);

        }

        private void gridLookUp_MaLop_EditValueChanged(object sender, EventArgs e)
        {
            loadPCDT_GV();
        }

        private void gridLookUp_MaLoaiDT_EditValueChanged(object sender, EventArgs e)
        {
            loadPCDT_GV();
        }
        private float DiemNHD;
        private float DiemPB;
        private string ID_PCDT;
        public void CapNhat()
        {
            //  if (!KTDulieu())
            // {
            //    XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
            //    return;
            // }
            DTO_QLPhanCongDT PCDT = new DTO_QLPhanCongDT();

        
            DiemNHD = float.Parse(gv_DA_Dalam.GetRowCellValue(gv_DA_Dalam.FocusedRowHandle, "DiemNHD").ToString());
            DiemPB = float.Parse(gv_DA_Dalam.GetRowCellValue(gv_DA_Dalam.FocusedRowHandle, "DiemPB").ToString());
            ID_PCDT = gv_DA_Dalam.GetRowCellValue(gv_DA_Dalam.FocusedRowHandle, "ID_PCDT").ToString();
            if (BUS_QLPhanCongDT.UpdateDiem(DiemNHD, DiemPB, ID_PCDT))
            {
                load();
                loadPCDT_GV();
            }
            else
                XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");

        }
        private void simpleBt_Capnhat_Click(object sender, EventArgs e)
        {
            CapNhat();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        }
    }
